﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%@ Import Namespace="OAclient.Common" %>
<script type="text/javascript">
    //更新table内容
    function updateTxlTable(obj) {
        var txlgroupid = $(obj).children("#txlgroupid").val();
        var txlgrouptype = $("#txlgrouptypelist").val();
        //alert(txlgroupid);
        $.ajax({
            type: "GET",
            url: "/Txl/TxlTable",
            data: { txlgroupid: txlgroupid, txlgrouptype: txlgrouptype },
            success: function(data) {
                $("#selftxlmrg_content").html(data);
            }
        })
    }

    //刷新table
    function refreahtable() {
        var txlgrouptype = $("#curtxlgrouptype").val();

        $.ajax({
            type: "GET",
            url: "/Txl/TxlGroupTable",
            data: { txlgrouptype: txlgrouptype },
            success: function(data) {
                $("#selftxlmrg_content").html(data);
            }
        })
    }

    //txlgroup
    function TxlGroupAdd() {
        var group_name = $("#txlgroupadd_name").val();
        if (group_name == "") {
            alert("请填写小组名");
            return;
        }

        $.ajax({
            type: "POST",
            url: "/Txl/DoTxlGroupAdd",
            data: { "group_name": group_name },
            success: function(restag) {
                if (!restag.res) {
                    alert(restag.msg);
                }
                else {
                    alert("操作成功！");
                    $('#menu_selftxlmrg').trigger('click');
                }
            }
        })
    }

    function TxlGroupUpdate() {
        var group_name = $("#txlgroupupdate_name").val();
        var txlgroup_id = $("#txlgroupupdate_id").val();
        $.ajax({
            type: "POST",
            url: "/Txl/DoTxlGroupUpdate",
            data: { "txlgroup_id": txlgroup_id, "txlgroup_name": group_name },
            success: function(restag) {
                if (!restag.res) {
                    alert(restag.msg);
                }
                else {
                    alert("操作成功！");
                    $('#menu_selftxlmrg').trigger('click');
                }
            }

        })
    }

    function TxlGroupDel(txlgroup_id) {

        if (txlgroup_id == "") {
            return;
        }
        if (txlgroup_id == 0) {
            return;
        }
        $.ajax({
            dataType: "json",
            type: "POST",
            url: "/Txl/DoTxlGroupDel",
            data: { "txlgroup_id": txlgroup_id },
            success: function(restag) {
                if (!restag.res) {
                    alert(restag.msg);
                }
                else {
                    alert("操作成功！");
                    $('#menu_selftxlmrg').trigger('click');
                }
            }
        });
    }

    //div显示
    function showTxlGroupUpdate(txlgroupid, txlgroupname) {
        //alert(txlgroupid + txlgroupname);
        $("#txlgroupupdate_id").val(txlgroupid);
        $("#txlgroupupdate_name").val(txlgroupname);
        $("#div_txlgroupupdate").show();
    }


    function proRes(ajaxContext) {
        var restag = {};
        try {
            restag = ajaxContext.get_response().get_object();
        } catch (e) {
            alert("程序出现错误，请联系管理员");
            return;  // return if result is not json
        }

        if (restag.res != true) {
            alert(restag.msg);
        }
        else {
            alert("操作成功");
            refreahtable();
        }
    }
</script>

<div id="txlgrouptable" style="margin-top: 20px;">
    <div>
        <% if (ViewData["txlgrouptype"] == "我的通讯录")
           { %>
        <input type="button" onclick="$('#div_txlgroupadd').show();" value="添加分组" style="margin-right: 7px;" /><!--增加右边界-->
        <%} %>
        <%=Html.Hidden("curtxlgrouptype",ViewData["txlgrouptype"]) %>
    </div>
    <table border="0" width="100%" class="table_style" cellpadding="0" cellspacing="0">
        <tr>
            <th>
                分组名称
            </th>
            <th>
                操作
            </th>
        </tr>
        <%
            System.Data.DataTable dt_txlgroup = ViewData["dt_txlgroup"] as System.Data.DataTable;
            var txlgrouptype = ViewData["txlgrouptype"];
            if (dt_txlgroup.Rows == null || dt_txlgroup.Rows.Count == 0)
            {%>
        <tr>
            <td colspan="0" style="text-align: center;">
                当前无数据！
            </td>
        </tr>
        <%
            }
            else
            {
                for (int i = 0; i < dt_txlgroup.Rows.Count; i++)
                {
                
        %>
        <tr>
            <td>
                <a onclick="updateTxlTable(this)">
                    <%=dt_txlgroup.Rows[i]["名称"].ToString()%>
                    <%=Html.Hidden("txlgroupid",dt_txlgroup.Rows[i]["小组编号"]) %>
                </a>
            </td>
            <td>
                <%if (txlgrouptype == "我的通讯录")
                  {%>
                <a title="编辑" onclick="showTxlGroupUpdate('<%=dt_txlgroup.Rows[i]["小组编号"].ToString()%>','<%=dt_txlgroup.Rows[i]["名称"].ToString()%>')">
                    编辑</a>
                <%=Ajax.ActionLink("删除", "DoTxlGroupDel",
                                                                              new { txlgroup_id = dt_txlgroup.Rows[i]["小组编号"] },
                    new AjaxOptions { UpdateTargetId = "tmp", OnSuccess = "proRes" })%>
                <%} %>
            </td>
        </tr>
        <%
            }
            } %>
    </table>
    <div id="pageLink">
        
        <%var pageInfo = ViewData["pageInfo"] as PagingInfo;%>
        <%if (pageInfo != null)
          { %>
        <%=Ajax.PageLinks(pageInfo, index => Url.Action("TxlGroupTable", new
        {
            page = index,
        }), new AjaxOptions { UpdateTargetId = "selftxlmrg_content" })%>
        <%} %>
    </div>
    <div id="div_txlgroupadd" style="display: none;">
        <input type="text" id="txlgroupadd_name" />
        <input type="button" value="添加" onclick="TxlGroupAdd()" />
        <input type="button" value="取消" onclick="$('#div_txlgroupadd').hide();" />
    </div>
    <div id="div_txlgroupupdate" style="display: none;">
        <input type="hidden" id="txlgroupupdate_id" />
        <input type="text" id="txlgroupupdate_name" />
        <input type="button" value="保存" onclick="TxlGroupUpdate()" />
        <input type="button" value="取消" onclick="$('#div_txlgroupupdate').hide();" />
    </div>
</div>
